import axios from "axios";
import { httpApi, geoApi } from '../config/config'

import { Loading } from 'element-ui';
let loading;

const startLoading = () => {  // 使用Element loading-start 方法
    loading = Loading.service({
        lock: true,
        background: 'rgba(0, 0, 0, 0.5)'
    });
};

//获得token
// import { getToken } from "../util/token";

const $http = axios.create({
    baseURL: httpApi,
    timeout: 600000,
});

const $httpGeo = axios.create({
    baseURL: geoApi,
    timeout: 600000,
});

const setAxios = (name, type) => {
    // 添加请求拦截器
    name.interceptors.request.use(
        function (config) {

            startLoading();
            // 在发送请求之前做些什么
            // config.headers.Authorization = getToken("access_token");
            if (type === 'geo') {
                config.headers = { 'Content-Type': 'text/plain; charset=UTF-8' }
            } else {
                config.headers.Authorizatio = window.localStorage.getItem('token')
            }

            return config;
        },
        function (error) {

            loading.close()
            // 对请求错误做些什么
            return Promise.reject(error);
        }
    );

    // 添加响应拦截器
    name.interceptors.response.use(
        function (response) {

            loading.close()

            // 对响应数据做点什么
            if (type === 'geo') {
                return response
            } else {
                return response.data;
            }
        },
        function (error) {

            loading.close()

            // 对响应错误做点什么
            return Promise.reject(error);
        }
    );
}

setAxios($http, 'web')
setAxios($httpGeo, 'geo')

export { $http, $httpGeo };